package com.lebang.controller.webapi;

import com.lebang.component.RedisClient;
import com.lebang.entity.RequestHeaders;
import com.lebang.model.MemberRecordModel;
import com.lebang.model.WebTask;
import com.lebang.model.enums.buss.TaskCategory;
import com.lebang.service.TaskClassifyService;
import com.lebang.service.TaskTagService;
import com.lebang.service.WebApiService;
import com.lebang.service.buss.MemberRecordService;
import com.lebang.service.buss.WalletService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: hwq
 * @Date: 2019/8/7 16:04
 * @Version 1.0
 */
@RestController
@RequestMapping("/web")
public class WebApi {
    @Autowired
    private WebApiService webApiService;
    @Autowired
    private MemberRecordService memberRecordService;

    @Autowired
    private WalletService walletService;

    /**
     * web任务列表
     *
     * @param request
     * @return
     */
    @PostMapping("/task/list")
    public JsonData taskList(HttpServletRequest request) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        String userid = requestHeaders.getUserid();
        String brand = requestHeaders.getBrand();
        String appId = requestHeaders.getAppid();
        int userAllHistoryMoney = walletService.totalMoney(userid, appId);
        List<WebTask> taskList = webApiService.getTaskList(userid, brand, userAllHistoryMoney);
        taskList = taskList.stream().filter(vo -> !vo.getCategory().equals(TaskCategory.Soso)).collect(Collectors.toList());
        return Callback.success("success", "查询成功", taskList);
    }

    /**
     * 任务完成记录
     *
     * @param request
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("/task/history")
    public JsonData history(HttpServletRequest request, Long page, Integer limit) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        String userid = requestHeaders.getUserid();
        List<MemberRecordModel> logs = webApiService.getTaskHistory(userid, page, limit);
        return Callback.success("success", "查询成功", logs);
    }



    @PostMapping("/member/cash/update")
    @ResponseBody
    public JsonData cash(String phone) {
        memberRecordService.updateCash(phone);
        return Callback.success("success", "查询成功", "");
    }

    /**
     * web任务列表
     *
     * @param request
     * @return
     */
    @PostMapping("/task/soso/list")
    public JsonData tasksosoList(HttpServletRequest request) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        String userid = requestHeaders.getUserid();
        String brand = requestHeaders.getBrand();
        String appId = requestHeaders.getAppid();
        int userAllHistoryMoney = walletService.totalMoney(userid, appId);
        List<WebTask> taskList = webApiService.getTaskList(userid, brand, userAllHistoryMoney);
        taskList = taskList.stream().filter(vo -> vo.getCategory().equals(TaskCategory.Soso)).collect(Collectors.toList());
        return Callback.success("success", "查询成功", taskList);
    }
}
